package cn.com.dhcc.turbo.service.monitor.gather;

import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.com.dhcc.app.core.exception.ServiceException;
import cn.com.dhcc.app.core.handler.modulehandler.WhereCondition;
import cn.com.dhcc.app.core.service.Log;
import cn.com.dhcc.app.core.util.CollectionUtil;
import cn.com.dhcc.app.core.util.UUIDGenerator;
import cn.com.dhcc.app.pub.core.consts.AppConst;
import cn.com.dhcc.turbo.entity.monitor.QuotaValueOnTime;
import cn.com.dhcc.turbo.entity.registry.AdapterInfo;
import cn.com.dhcc.turbo.entity.registry.DomainInfo;
import cn.com.dhcc.turbo.entity.registry.NodeInfo;
import cn.com.dhcc.turbo.service.exch.cmdata.RecvCmDataService;
import cn.com.dhcc.turbo.service.exch.cmdata.SendCmDataService;
import cn.com.dhcc.turbo.service.monitor.MonitorService;
import cn.com.dhcc.turbo.service.monitor.QUOTA;
import cn.com.dhcc.turbo.service.registry.AdapterInfoService;
import cn.com.dhcc.turbo.service.registry.DomainInfoService;
import cn.com.dhcc.turbo.service.registry.NodeInfoService;
/**
 * 常规数据监控统计指标
 */
@Service
public class CmDataRealtimeGatherService {

	@SuppressWarnings("deprecation")
	Logger logger = Log.getExistLogger(Log.LOGTYPE.MONITOR);
	@Autowired
	private SendCmDataService sendCmDataService;
	@Autowired
	private RecvCmDataService recvCmDataService;
	@Autowired
	NodeInfoService nodeInfoService;
	
	@Autowired
	MonitorService monitorService;
	@Autowired
	QuotaGatherUtil quotaGatherUtil;
	@Autowired
	DomainInfoService domainInfoService;
	@Autowired
	AdapterInfoService adapterService;
	/**
	 * 发送统计，按照接收系统和计算时间频度之间的待发送的数量
	 * @param interval
	 */
	public void cmStructuredSendWaitNumForAdapter() {
		QUOTA quota = QUOTA.STRUCTURED_SEND_WAIT_NUM_FOR_ADAPTER;
		int num = 0;
		logger.info("开始收集指标[" + quota.getDesc() + "]...");
		try {
			List<AdapterInfo> adapterList = adapterService.getLocalAdapterList();
			Date now = new Date();
			
			for (AdapterInfo adapter : adapterList) {
				String code = adapter.getAdapterCode();
				WhereCondition searchData = new WhereCondition();
				searchData
						.where()
						.eq("data_type", AppConst.ExchDataType.CM_DATA.value())
						.and()
						.eq("send_status", AppConst.SendStatus.READY.value())
						.and()
						.eq("send_adaptorcode", code);
						
						
				num = sendCmDataService.count(searchData);
				QuotaValueOnTime onTime = new QuotaValueOnTime();
				onTime.setQuotaCode(quota.getCode());
				onTime.setMtime(now);
				onTime.setId(UUIDGenerator.getUUID());
				onTime.setStatus(QuotaValueOnTime.QuotaHadlerStatus.WAIT_HANDLER.getCode());
				onTime.setOwnerId(adapter.getId());
				onTime.setCurrentValue1("" + num);
				monitorService.insertQuotaOnTime(onTime);
			}
		} catch (ServiceException e) {
			logger.error("指标[" + quota.getDesc() + "]值收集失败", e);
		}
		logger.info("指标[" + quota.getDesc() + "]值收集结束，值为：" + num);
	}
	
	
	/**
	 * 发送统计，按照接收系统和计算时间频度之间的发送中的数量
	 * @param interval
	 */
	public void cmStructuredSendSendingNumForAdapter() {
		QUOTA quota = QUOTA.STRUCTURED_SEND_SENDING_NUM_FOR_ADAPTER;
		int num = 0;
		logger.info("开始收集指标[" + quota.getDesc() + "]...");
		try {
			List<AdapterInfo> adapterList = adapterService.getLocalAdapterList();
			Date now = new Date();
			String status[] = {"11","12","21","22","31","32","33","41","42","43","51","52"};
			for (AdapterInfo adapter : adapterList) {
				String code = adapter.getAdapterCode();
				WhereCondition searchData = new WhereCondition();
				searchData
				.where()
				.eq("data_type", AppConst.ExchDataType.CM_DATA.value())
				.and().in("send_status", status)
				.and()
				.eq("send_adaptorcode", code);
				
				num = sendCmDataService.count(searchData);
				QuotaValueOnTime onTime = new QuotaValueOnTime();
				onTime.setQuotaCode(quota.getCode());
				onTime.setMtime(now);
				onTime.setId(UUIDGenerator.getUUID());
				onTime.setStatus(QuotaValueOnTime.QuotaHadlerStatus.WAIT_HANDLER.getCode());
				onTime.setOwnerId(adapter.getId());
				onTime.setCurrentValue1("" + num);
				monitorService.insertQuotaOnTime(onTime);
			}
		} catch (ServiceException e) {
			logger.error("指标[" + quota.getDesc() + "]值收集失败", e);
		}
		logger.info("指标[" + quota.getDesc() + "]值收集结束，值为：" + num);
	}
	
	
	
	/**
	 * 发送统计，按照接收系统和计算时间频度之间的发送成功的数量
	 * @param interval
	 */
	public void cmStructuredSendSuccessNumForAdapter() {
		QUOTA quota = QUOTA.STRUCTURED_SEND_SUCCESS_NUM_FOR_ADAPTER;
		int num = 0;
		logger.info("开始收集指标[" + quota.getDesc() + "]...");
		try {
			List<AdapterInfo> adapterList = adapterService.getLocalAdapterList();
			Date now = new Date();
			String status[] = {"1","33"};
			for (AdapterInfo adapter : adapterList) {
				String code = adapter.getAdapterCode();
				WhereCondition searchData = new WhereCondition();
				searchData
						.where()
						.eq("data_type", AppConst.ExchDataType.CM_DATA.value())
						.and()
						.in("send_status", status)
						.and()
						.eq("send_adaptorcode", code);
						
				num = sendCmDataService.count(searchData);
				QuotaValueOnTime onTime = new QuotaValueOnTime();
				onTime.setQuotaCode(quota.getCode());
				onTime.setMtime(now);
				onTime.setId(UUIDGenerator.getUUID());
				onTime.setStatus(QuotaValueOnTime.QuotaHadlerStatus.WAIT_HANDLER.getCode());
				onTime.setOwnerId(adapter.getId());
				onTime.setCurrentValue1("" + num);
				monitorService.insertQuotaOnTime(onTime);
			}
		} catch (ServiceException e) {
			logger.error("指标[" + quota.getDesc() + "]值收集失败", e);
		}
		logger.info("指标[" + quota.getDesc() + "]值收集结束，值为：" + num);
	}
	
	
	/**
	 * 发送统计，按照接收系统和计算时间频度之间的发送失败的数量
	 * @param interval
	 */
	public void cmStructuredSendFailNumForAdapter() {
		QUOTA quota = QUOTA.STRUCTURED_SEND_FAIL_NUM_FOR_ADAPTER;
		int num = 0;
		logger.info("开始收集指标[" + quota.getDesc() + "]...");
		try {
			List<AdapterInfo> adapterList = adapterService.getLocalAdapterList();
			Date now = new Date();
			
			for (AdapterInfo adapter : adapterList) {
				String code = adapter.getAdapterCode();
				WhereCondition searchData = new WhereCondition();
				searchData
				.where()
				.eq("data_type", AppConst.ExchDataType.CM_DATA.value())
				.and()
				.eq("send_status", AppConst.SendStatus.FAIL.value())
				.and()
				.eq("send_adaptorcode", code);
				
				num = sendCmDataService.count(searchData);
				QuotaValueOnTime onTime = new QuotaValueOnTime();
				onTime.setQuotaCode(quota.getCode());
				onTime.setMtime(now);
				onTime.setId(UUIDGenerator.getUUID());
				onTime.setStatus(QuotaValueOnTime.QuotaHadlerStatus.WAIT_HANDLER.getCode());
				onTime.setOwnerId(adapter.getId());
				onTime.setCurrentValue1("" + num);
				monitorService.insertQuotaOnTime(onTime);
			}
		} catch (ServiceException e) {
			logger.error("指标[" + quota.getDesc() + "]值收集失败", e);
		}
		logger.info("指标[" + quota.getDesc() + "]值收集结束，值为：" + num);
	}
	/**
	 * 发送统计，按照接收系统和计算时间频度之间的发送成功率
	 * @param interval
	 */
	public void cmStructuredSendSuccessRateForAdapter() {
		QUOTA quota = QUOTA.STRUCTURED_SEND_SUCCESS_RATE_FOR_ADAPTER;
		int wait = 0;
		int sending = 0;
		int success = 0;
		int fail = 0;
		int sum = 0;
        String rate = "" ;
		logger.info("开始收集指标[" + quota.getDesc() + "]...");
		try {
			List<AdapterInfo> adapterList = adapterService.getLocalAdapterList();
			Date now = new Date();
			String sendingStatus[] = {"11","12","21","22","31","32","33","41","42","43","51","52"};
			String successStatus[] = {"1","33"};
			for (AdapterInfo adapter : adapterList) {
				String code = adapter.getAdapterCode();
				WhereCondition searchData = new WhereCondition();
				searchData.where().eq("data_type", AppConst.ExchDataType.CM_DATA.value()).and().eq("send_adaptorcode", code);
				
				WhereCondition waitSearchData = new WhereCondition();
				WhereCondition successSearchData = new WhereCondition();
				WhereCondition sendingSearchData = new WhereCondition();
				WhereCondition failSearchData = new WhereCondition();
				
				waitSearchData.where().eq("data_type", AppConst.ExchDataType.CM_DATA.value()).and().eq("send_adaptorcode", code);
				successSearchData.where().eq("data_type", AppConst.ExchDataType.CM_DATA.value()).and().eq("send_adaptorcode", code);
				sendingSearchData.where().eq("data_type", AppConst.ExchDataType.CM_DATA.value()).and().eq("send_adaptorcode", code);
				failSearchData.where().eq("data_type", AppConst.ExchDataType.CM_DATA.value()).and().eq("send_adaptorcode", code);
				
				
				wait = sendCmDataService.count(waitSearchData.and().eq("send_status", AppConst.SendStatus.READY.value()));
				sending = sendCmDataService.count(sendingSearchData.and().in("send_status", sendingStatus));
				success = sendCmDataService.count(successSearchData.and().in("send_status",successStatus));
				fail = sendCmDataService.count(failSearchData.and().eq("send_status", AppConst.SendStatus.FAIL.value()));
				sum = wait+sending+success+fail;
				if(sum >0){
					rate = Math.round(Math.floor((float)success/sum*100))+"";
				}else{
					rate = "0";
				}
				QuotaValueOnTime onTime = new QuotaValueOnTime();
				onTime.setQuotaCode(quota.getCode());
				onTime.setMtime(now);
				onTime.setId(UUIDGenerator.getUUID());
				onTime.setStatus(QuotaValueOnTime.QuotaHadlerStatus.WAIT_HANDLER.getCode());
				onTime.setOwnerId(adapter.getId());
				onTime.setCurrentValue1(rate);
				monitorService.insertQuotaOnTime(onTime);
			}
		} catch (ServiceException e) {
			logger.error("指标[" + quota.getDesc() + "]值收集失败", e);
		}
		logger.info("指标[" + quota.getDesc() + "]值收集结束，值为：" + rate);
	}
	
	
	
	
	
	/**
	 * 接收统计，按照接收系统和计算时间频度之间的接收成功的数量
	 * @param interval
	 */
	public void cmStructuredRecvSuccessNumForAdapter() {
		QUOTA quota = QUOTA.STRUCTURED_RECV_SUCCESS_NUM_FOR_ADAPTER;
		int num = 0;
		logger.info("开始收集指标[" + quota.getDesc() + "]...");
		try {
			List<AdapterInfo> adapterList = adapterService.getLocalAdapterList();
			Date now = new Date();
			
			for (AdapterInfo adapter : adapterList) {
				String code = adapter.getAdapterCode();
				WhereCondition searchData = new WhereCondition();
				searchData
				.where()
				.eq("data_type", AppConst.ExchDataType.CM_DATA.value())
				.and()
				.eq("send_status", AppConst.SendStatus.SENT.value())
				.and()
				.eq("recv_adaptorcode", code);
				
				
				num = recvCmDataService.count(searchData);
				QuotaValueOnTime onTime = new QuotaValueOnTime();
				onTime.setQuotaCode(quota.getCode());
				onTime.setMtime(now);
				onTime.setId(UUIDGenerator.getUUID());
				onTime.setStatus(QuotaValueOnTime.QuotaHadlerStatus.WAIT_HANDLER.getCode());
				onTime.setOwnerId(adapter.getId());
				onTime.setCurrentValue1("" + num);
				monitorService.insertQuotaOnTime(onTime);
			}
		} catch (ServiceException e) {
			logger.error("指标[" + quota.getDesc() + "]值收集失败", e);
		}
		logger.info("指标[" + quota.getDesc() + "]值收集结束，值为：" + num);
	}
	
	
	/**
	 * 接收统计，按照接收系统和计算时间频度之间的接收失败的数量
	 * @param interval
	 */
	public void cmStructuredRecvFailNumForAdapter() {
		QUOTA quota = QUOTA.STRUCTURED_RECV_FAIL_NUM_FOR_ADAPTER;
		int num = 0;
		logger.info("开始收集指标[" + quota.getDesc() + "]...");
		try {
			List<AdapterInfo> adapterList = adapterService.getLocalAdapterList();
			Date now = new Date();
			
			for (AdapterInfo adapter : adapterList) {
				String code = adapter.getAdapterCode();
				WhereCondition searchData = new WhereCondition();
				searchData
				.where()
				.eq("data_type", AppConst.ExchDataType.CM_DATA.value())
				.and()
				.eq("send_status", AppConst.SendStatus.FAIL.value())
				.and()
				.eq("recv_adaptorcode", code);
				
				num = recvCmDataService.count(searchData);
				QuotaValueOnTime onTime = new QuotaValueOnTime();
				onTime.setQuotaCode(quota.getCode());
				onTime.setMtime(now);
				onTime.setId(UUIDGenerator.getUUID());
				onTime.setStatus(QuotaValueOnTime.QuotaHadlerStatus.WAIT_HANDLER.getCode());
				onTime.setOwnerId(adapter.getId());
				onTime.setCurrentValue1("" + num);
				monitorService.insertQuotaOnTime(onTime);
			}
		} catch (ServiceException e) {
			logger.error("指标[" + quota.getDesc() + "]值收集失败", e);
		}
		logger.info("指标[" + quota.getDesc() + "]值收集结束，值为：" + num);
	}
	/**
	 * 接收统计，按照接收系统和计算时间频度之间的接收成功率
	 * @param interval
	 */
	public void cmStructuredRecvSuccessRateForAdapter() {
		QUOTA quota = QUOTA.STRUCTURED_RECV_SUCCESS_RATE_FOR_ADAPTER;
		int success = 0;
		int fail = 0;
		int sum = 0;
        String rate = "" ;
		logger.info("开始收集指标[" + quota.getDesc() + "]...");
		try {
			List<AdapterInfo> adapterList = adapterService.getLocalAdapterList();
			Date now = new Date();
			
			for (AdapterInfo adapter : adapterList) {
				String code = adapter.getAdapterCode();
				WhereCondition searchData = new WhereCondition();
				searchData.where().eq("data_type", AppConst.ExchDataType.CM_DATA.value()).and().eq("recv_adaptorcode", code);
				
				WhereCondition successSearchData = new WhereCondition();
				WhereCondition failSearchData = new WhereCondition();
				
				successSearchData.where().eq("data_type", AppConst.ExchDataType.CM_DATA.value()).and().eq("recv_adaptorcode", code);
				failSearchData.where().eq("data_type", AppConst.ExchDataType.CM_DATA.value()).and().eq("recv_adaptorcode", code);
				
				
				success = recvCmDataService.count(successSearchData.and().eq("send_status", AppConst.SendStatus.SENT.value()));
				fail = recvCmDataService.count(failSearchData.and().eq("send_status", AppConst.SendStatus.FAIL.value()));
				sum = success+fail;
				if(sum >0){
					rate = Math.round(Math.floor((float)success/sum*100))+"";
				}else{
					rate = "0";
				}
				QuotaValueOnTime onTime = new QuotaValueOnTime();
				onTime.setQuotaCode(quota.getCode());
				onTime.setMtime(now);
				onTime.setId(UUIDGenerator.getUUID());
				onTime.setStatus(QuotaValueOnTime.QuotaHadlerStatus.WAIT_HANDLER.getCode());
				onTime.setOwnerId(adapter.getId());
				onTime.setCurrentValue1(rate);
				monitorService.insertQuotaOnTime(onTime);
			}
		} catch (ServiceException e) {
			logger.error("指标[" + quota.getDesc() + "]值收集失败", e);
		}
		logger.info("指标[" + quota.getDesc() + "]值收集结束，值为：" + rate);
	}
	/**
	 * 节点发送成功率
	 */
	public void cmStructuredSendSuccessRateForNode() {
		QUOTA quota = QUOTA.STRUCTURED_SEND_SUCCESS_RATE_FOR_NODE;
		int wait = 0;
		int sending = 0;
		int success = 0;
		int fail = 0;
		int sum = 0;
        String rate = "" ;
		logger.info("开始收集指标[" + quota.getDesc() + "]...");
		try {
			Date now = new Date();
			NodeInfo localNode = nodeInfoService.getLocalNode();
			if(localNode==null){
				logger.error("自动根据系统指标值计算节点指标指标失败:节点未初始化");
				return;
			}
			//待发送
			List<QuotaValueOnTime> waitValueOnTimes =	monitorService.getLastQuotaValueByQuotaCode(QUOTA.STRUCTURED_SEND_WAIT_NUM_FOR_NODE.getCode());
			if(CollectionUtil.hasElement(waitValueOnTimes)){
				wait =  Integer.parseInt(waitValueOnTimes.get(0).getCurrentValue1());
			}
			//发送中
			List<QuotaValueOnTime> sendingValueOnTimes =	monitorService.getLastQuotaValueByQuotaCode(QUOTA.STRUCTURED_SEND_SENDING_NUM_FOR_NODE.getCode());
			if(CollectionUtil.hasElement(sendingValueOnTimes)){
				sending =  Integer.parseInt(sendingValueOnTimes.get(0).getCurrentValue1());
			}
			//发送成功
			List<QuotaValueOnTime> successValueOnTimes =	monitorService.getLastQuotaValueByQuotaCode(QUOTA.STRUCTURED_SEND_SUCCESS_NUM_FOR_NODE.getCode());
			if(CollectionUtil.hasElement(successValueOnTimes)){
				success =  Integer.parseInt(successValueOnTimes.get(0).getCurrentValue1());
			}
			//发送失败
			List<QuotaValueOnTime> failValueOnTimes =	monitorService.getLastQuotaValueByQuotaCode(QUOTA.STRUCTURED_SEND_FAIL_NUM_FOR_NODE.getCode());
			if(CollectionUtil.hasElement(failValueOnTimes)){
				fail =  Integer.parseInt(failValueOnTimes.get(0).getCurrentValue1());
			}
			sum = wait+sending+success+fail;
			if(sum >0){
				rate = Math.round(Math.floor((float)success/sum*100))+"";
			}else{
				rate = "0";
			}
			QuotaValueOnTime onTime = new QuotaValueOnTime();
			onTime.setQuotaCode(quota.getCode());
			onTime.setMtime(now);
			onTime.setId(UUIDGenerator.getUUID());
			onTime.setStatus(QuotaValueOnTime.QuotaHadlerStatus.WAIT_HANDLER.getCode());
			onTime.setOwnerId(localNode.getId());
			onTime.setCurrentValue1(rate);
			monitorService.insertQuotaOnTime(onTime);
		} catch (ServiceException e) {
			logger.error("指标[" + quota.getDesc() + "]值收集失败", e);
		}
		logger.info("指标[" + quota.getDesc() + "]值收集结束，值为：" + rate);
	}
	/**
	 * 节点接收成功率
	 */
	public void cmStructuredRecvSuccessRateForNode() {
		QUOTA quota = QUOTA.STRUCTURED_RECV_SUCCESS_RATE_FOR_NODE;
		int success = 0;
		int fail = 0;
		int sum = 0;
        String rate = "" ;
		logger.info("开始收集指标[" + quota.getDesc() + "]...");
		try {
			Date now = new Date();
			NodeInfo localNode = nodeInfoService.getLocalNode();
			if(localNode==null){
				logger.error("自动根据系统指标值计算节点指标指标失败:节点未初始化");
				return;
			}
			//接收成功
			List<QuotaValueOnTime> successValueOnTimes =	monitorService.getLastQuotaValueByQuotaCode(QUOTA.STRUCTURED_RECV_SUCCESS_NUM_FOR_NODE.getCode());
			if(CollectionUtil.hasElement(successValueOnTimes)){
				success =  Integer.parseInt(successValueOnTimes.get(0).getCurrentValue1());
			}
			//接收失败
			List<QuotaValueOnTime> failValueOnTimes =	monitorService.getLastQuotaValueByQuotaCode(QUOTA.STRUCTURED_RECV_FAIL_NUM_FOR_NODE.getCode());
			if(CollectionUtil.hasElement(failValueOnTimes)){
				fail =  Integer.parseInt(failValueOnTimes.get(0).getCurrentValue1());
			}
			sum = success+fail;
			if(sum >0){
				rate = Math.round(Math.floor((float)success/sum*100))+"";
			}else{
				rate = "0";
			}
			QuotaValueOnTime onTime = new QuotaValueOnTime();
			onTime.setQuotaCode(quota.getCode());
			onTime.setMtime(now);
			onTime.setId(UUIDGenerator.getUUID());
			onTime.setStatus(QuotaValueOnTime.QuotaHadlerStatus.WAIT_HANDLER.getCode());
			onTime.setOwnerId(localNode.getId());
			onTime.setCurrentValue1(rate);
			monitorService.insertQuotaOnTime(onTime);
		} catch (ServiceException e) {
			logger.error("指标[" + quota.getDesc() + "]值收集失败", e);
		}
		logger.info("指标[" + quota.getDesc() + "]值收集结束，值为：" + rate);
	}
	
	/**
	 * 域发送成功率
	 */
	public void cmStructuredSendSuccessRateForDomain() {
		QUOTA quota = QUOTA.STRUCTURED_SEND_SUCCESS_RATE_FOR_DOMAIN;
		int wait = 0;
		int sending = 0;
		int success = 0;
		int fail = 0;
		int sum = 0;
        String rate = "" ;
		logger.info("开始收集指标[" + quota.getDesc() + "]...");
		try {
			Date now = new Date();
			List<DomainInfo> domainInfos = domainInfoService.list(null);
			if(CollectionUtil.hasElement(domainInfos)){
				return;
			}
			for (Iterator<DomainInfo> iterator = domainInfos.iterator(); iterator.hasNext();) {
				DomainInfo domainInfo =  iterator.next();
				String domainId = domainInfo.getId();
				//待发送
				List<QuotaValueOnTime> waitValueOnTimes =	monitorService.getQuotaValueOnTimeByQuotaCodeAndOwnerId(QUOTA.STRUCTURED_SEND_WAIT_NUM_FOR_DOMAIN.getCode(),domainId);
				if(CollectionUtil.hasElement(waitValueOnTimes)){
					wait =  Integer.parseInt(waitValueOnTimes.get(0).getCurrentValue1());
				}
				//发送中
				List<QuotaValueOnTime> sendingValueOnTimes =	monitorService.getQuotaValueOnTimeByQuotaCodeAndOwnerId(QUOTA.STRUCTURED_SEND_SENDING_NUM_FOR_DOMAIN.getCode(),domainId);
				if(CollectionUtil.hasElement(sendingValueOnTimes)){
					sending =  Integer.parseInt(sendingValueOnTimes.get(0).getCurrentValue1());
				}
				//发送成功
				List<QuotaValueOnTime> successValueOnTimes =	monitorService.getQuotaValueOnTimeByQuotaCodeAndOwnerId(QUOTA.STRUCTURED_SEND_SUCCESS_NUM_FOR_DOMAIN.getCode(),domainId);
				if(CollectionUtil.hasElement(successValueOnTimes)){
					success =  Integer.parseInt(successValueOnTimes.get(0).getCurrentValue1());
				}
				//发送失败
				List<QuotaValueOnTime> failValueOnTimes =	monitorService.getQuotaValueOnTimeByQuotaCodeAndOwnerId(QUOTA.STRUCTURED_SEND_FAIL_NUM_FOR_DOMAIN.getCode(),domainId);
				if(CollectionUtil.hasElement(failValueOnTimes)){
					fail =  Integer.parseInt(failValueOnTimes.get(0).getCurrentValue1());
				}
				sum = wait+sending+success+fail;
				if(sum >0){
					rate = Math.round(Math.floor((float)success/sum*100))+"";
				}else{
					rate = "0";
				}
				QuotaValueOnTime onTime = new QuotaValueOnTime();
				onTime.setQuotaCode(quota.getCode());
				onTime.setMtime(now);
				onTime.setId(UUIDGenerator.getUUID());
				onTime.setStatus(QuotaValueOnTime.QuotaHadlerStatus.WAIT_HANDLER.getCode());
				onTime.setOwnerId(domainId);
				onTime.setCurrentValue1(rate);
				monitorService.insertQuotaOnTime(onTime);
			}
		} catch (ServiceException e) {
			logger.error("指标[" + quota.getDesc() + "]值收集失败", e);
		}
		logger.info("指标[" + quota.getDesc() + "]值收集结束，值为：" + rate);
	}
	/**
	 * 域接收成功率
	 */
	public void cmStructuredRecvSuccessRateForDomain() {
		QUOTA quota = QUOTA.STRUCTURED_RECV_SUCCESS_RATE_FOR_DOMAIN;
		int success = 0;
		int fail = 0;
		int sum = 0;
        String rate = "" ;
		logger.info("开始收集指标[" + quota.getDesc() + "]...");
		try {
			Date now = new Date();
			List<DomainInfo> domainInfos = domainInfoService.list(null);
			if(CollectionUtil.hasElement(domainInfos)){
				return;
			}
			for (Iterator<DomainInfo> iterator = domainInfos.iterator(); iterator.hasNext();) {
				DomainInfo domainInfo =  iterator.next();
				String domainId = domainInfo.getId();
				//接收成功
				List<QuotaValueOnTime> successValueOnTimes =	monitorService.getQuotaValueOnTimeByQuotaCodeAndOwnerId(QUOTA.STRUCTURED_RECV_SUCCESS_NUM_FOR_DOMAIN.getCode(),domainId);
				if(CollectionUtil.hasElement(successValueOnTimes)){
					success =  Integer.parseInt(successValueOnTimes.get(0).getCurrentValue1());
				}
				//接收失败
				List<QuotaValueOnTime> failValueOnTimes =	monitorService.getQuotaValueOnTimeByQuotaCodeAndOwnerId(QUOTA.STRUCTURED_RECV_FAIL_NUM_FOR_DOMAIN.getCode(),domainId);
				if(CollectionUtil.hasElement(failValueOnTimes)){
					fail =  Integer.parseInt(failValueOnTimes.get(0).getCurrentValue1());
				}
				sum = success+fail;
				if(sum >0){
					rate = Math.round(Math.floor((float)success/sum*100))+"";
				}else{
					rate = "0";
				}
				QuotaValueOnTime onTime = new QuotaValueOnTime();
				onTime.setQuotaCode(quota.getCode());
				onTime.setMtime(now);
				onTime.setId(UUIDGenerator.getUUID());
				onTime.setStatus(QuotaValueOnTime.QuotaHadlerStatus.WAIT_HANDLER.getCode());
				onTime.setOwnerId(domainId);
				onTime.setCurrentValue1(rate);
				monitorService.insertQuotaOnTime(onTime);
			}
			
		} catch (ServiceException e) {
			logger.error("指标[" + quota.getDesc() + "]值收集失败", e);
		}
		logger.info("指标[" + quota.getDesc() + "]值收集结束，值为：" + rate);
	}
    /**
     * 在收集完系统监控值后执行
     */
	public void autoGatherNodeAndDomainOntimeValue() {
		QUOTA adapterQuotas[] = { 
				QUOTA.STRUCTURED_SEND_WAIT_NUM_FOR_ADAPTER,
				QUOTA.STRUCTURED_SEND_SENDING_NUM_FOR_ADAPTER,
				QUOTA.STRUCTURED_SEND_SUCCESS_NUM_FOR_ADAPTER,
				QUOTA.STRUCTURED_SEND_FAIL_NUM_FOR_ADAPTER,
				QUOTA.STRUCTURED_RECV_SUCCESS_NUM_FOR_ADAPTER,
				QUOTA.STRUCTURED_RECV_FAIL_NUM_FOR_ADAPTER};
		try {
			NodeInfo localNode = nodeInfoService.getLocalNode();
			if(localNode==null){
				logger.error("自动根据系统指标值计算节点指标指标失败:节点未初始化");
				return;
			}
			quotaGatherUtil.gatherValueForNodeByAdapterLastValue(adapterQuotas);
			cmStructuredSendSuccessRateForNode();
			cmStructuredRecvSuccessRateForNode();
			quotaGatherUtil.gatherValueForDomainByAdapterLastValue(adapterQuotas);
			cmStructuredSendSuccessRateForDomain();
			cmStructuredRecvSuccessRateForDomain();
		} catch (ServiceException e) {
			logger.error("自动计算指标值失败!", e);
		}
	}
}
